package com.orchard.xpz.controller.login;

import com.orchard.xpz.entity.sysUser.SysUser;
import com.orchard.xpz.service.sysUser.SysUserService;
import com.orchard.xpz.utils.R;
import com.orchard.xpz.utils.UserRuntime;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @TaskCode:
 * @description:
 * @author: minshuai.chen
 * @create: 2022/4/14 20:48
 **/
@RestController
@RequestMapping("/")
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/login")
    public R<String> login(String userName, String passWord, HttpServletRequest request){
        if (ObjectUtils.isEmpty(userName) || ObjectUtils.isEmpty(passWord)){
            return R.fail("请输入用户名或者密码");
        }
        SysUser user = sysUserService.getUserByName(userName);
        if (passWord.equals(user.getPassword())){
            request.getSession().setAttribute("token",user);
            UserRuntime.setUser(user);
            Thread t = Thread.currentThread();
            System.out.println(t+"==" +t.getId()+"++"+ t.getId());
            System.out.println(UserRuntime.getUser().toString());
            return R.success("登录成功");
        }else {
            return R.fail("密码错误");
        }
    }

    @RequestMapping("/logOut")
    public R<String> logOut(){
        UserRuntime.remove();
        return R.success("退出成功");
    }
}
